// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import VueRouter from 'vue-router'
import App from './App'
import Home from './components/Home'
import Ranking from './components/Ranking'
import Spirit from './components/Spirit'
import Author from './components/Author'
import 'lib-flexible'
import {
    Loading,
    XProgress,
    ToastPlugin,
    ConfirmPlugin,
    LoadingPlugin,
    DevicePlugin,
    Selector
} from 'vux'
import axios from 'axios'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
Vue.prototype.$http = axios;
import store from './store/store.js';
import LuckyWheel from './components/Luckywheel';
import wechat from './libs/wechat';
import {
    picList
} from './config';
Vue.prototype.picList = picList;


Vue.use(LoadingPlugin);
Vue.use(DevicePlugin);
Vue.use(ConfirmPlugin);
Vue.use(VueRouter);
Vue.use(ToastPlugin, {
    position: 'top'
});
Vue.component('loading', Loading);
Vue.component('x-progress', XProgress);
Vue.component('selector', Selector)


const routes = [{
    path: '/',
    component: Home,
},
{
    path: '/spirit/:no',
    component: Spirit
},
{
    path: '/ranking',
    component: Ranking
},
{
    path: '/reward',
    component: LuckyWheel
},
{
    path: '/author',
    component: Author
}
];

const router = new VueRouter({
    mode: 'history',
    routes
});


window.localStorage.removeItem('token');
window.localStorage.removeItem('user');





// router.beforeRouteEnter((to, from, next) => {
//     // 修复iOS微信签名错误
//     if (from.path == '/partner') {
//       location.assign('http://crm.jdy.com/app/partner/productPromotion')
//     } else {
//       next()
//     }
//   });

router.beforeEach((to, from, next) => {
    
    // if (!Vue.device.isAndroid) {
    //     if (!store.state.iosJsUrl) {
    //         let landingPage = document.URL;
    //         console.log(landingPage);
    //         store.commit('SET_WX_JS_URL', {
    //             iosJsUrl: landingPage
    //         })
    //     }
    // }
    if (to.query.pId) {
        window.localStorage.setItem('pid', to.query.pId);
        axios.get('http://api.ittie.cn/api/user/pId?pId=' + to.query.pId).then(res => {
            let data = res.data;
            if (data.code == 200) {
                store.commit('SETSHAREINFO', {
                    id: data.data.id,
                    name: data.data.username
                });
            }
        })
    }
    if (!window.localStorage.getItem('user') && to.path != '/author') {
        window.localStorage.setItem('beforeLoginUrl', to.fullPath);
        next('/author');
        return false
    };
    if (to.path == '/author' && window.localStorage.getItem('user')) {
        next('/');
        return false
    };
    next()
});

router.afterEach(to => {
    // store.commit('UPDATE_LOADING_STATUS', {
    //     isLoading: false
    // })
    // let _url = window.location.origin + to.fullPath;
    if (!Vue.device.isAndroid) {
        // _url = store.state.iosJsUrl.split('#')[0]
    }
    // console.log(_url);
    // wechat.getJSSDK(_url)
})
//
// Vue.prototype.wxShare = wechat.setWxShare;

// 登录后跳转方法
Vue.prototype.goBeforeLoginUrl = () => {
    let url = window.localStorage.getItem('beforeLoginUrl');
    if (!url || url.indexOf('/author') != -1) {
        router.push('/')
    } else {
        router.push(url);
        window.localStorage.setItem('beforeLoginUrl', '');
    }
}
// FastClick.attach(document.body)

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
    router,
    store,
    render: h => h(App),
    // mounted() {
    //     this.$store.commit('UPDATE_DEVICE', {
    //         isAndroid: Vue.device.isAndroid
    //     })
    // }
}).$mount('#app-box')